package com.dz.Controller;

import com.dz.Service.BookService;
import com.dz.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookControlller {
    //controllerc层调Service层
    @Autowired
    //指定实现类
    @Qualifier("BookSreviceImpl")
     private BookService bookService;

    //查询所有的书籍，并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books = bookService.queryAllBooks();
        model.addAttribute("books",books);
        return "allBook";
    }
    //跳转到增加书籍页面
    @RequestMapping("/toAddbook")
    public String toAddBook(){
        return "addBook";
    }
     //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        //添加成功后，回到首页   重定向
        return "redirect:/book/allBook";
    }

    //跳转到修改页面
    @RequestMapping("/toupdata")
    public String toUpdatapage(String id,Model model){
        Books books = bookService.queryBook(id);
        model.addAttribute("books",books);
        return "updataBook";
    }
    //修改书籍
    @RequestMapping("updataBook")
    public String updataBook(Books books){
        System.out.println("updataBook="+books);
        bookService.updataBook(books);
        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/deleteBook")
    public String deleteBook(String id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    //查询书籍
    @RequestMapping("queryBook")
    public String queryBook(String queryBookByName,Model model){
        Books book = bookService.queryBookByName(queryBookByName);
        List<Books> books = new ArrayList<>();
        books.add(book);
        model.addAttribute("books",books);
        return "allBook";

    }

}
